package com.k8sops.backend.controller;

import com.k8sops.backend.common.DevOpsConstant;
import com.k8sops.backend.common.Result;
import com.k8sops.backend.entity.Role;
import com.k8sops.backend.service.RoleService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色管理控制器
 */
@RestController
@RequestMapping("/api/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 创建角色
     * @param role 角色信息
     * @return 创建结果
     */
    @PostMapping("/create")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR}, logical = Logical.OR)
    public Result<Role> createRole(@RequestBody Role role) {
        Role createdRole = roleService.createRole(role);
        return Result.success(createdRole);
    }

    /**
     * 更新角色
     * @param role 角色信息
     * @return 更新结果
     */
    @PutMapping("/update")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR}, logical = Logical.OR)
    public Result<Role> updateRole(@RequestBody Role role) {
        Role updatedRole = roleService.updateRole(role);
        return Result.success(updatedRole);
    }

    /**
     * 删除角色
     * @param roleId 角色ID
     * @return 删除结果
     */
    @DeleteMapping("/delete/{roleId}")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR}, logical = Logical.OR)
    public Result<Boolean> deleteRole(@PathVariable Long roleId) {
        boolean deleted = roleService.deleteRole(roleId);
        return Result.success(deleted);
    }

    /**
     * 获取角色详情
     * @param roleId 角色ID
     * @return 角色详情
     */
    @GetMapping("/detail/{roleId}")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR, DevOpsConstant.RoleCode.ADMINISTRATOR}, logical = Logical.OR)
    public Result<Role> getRoleDetail(@PathVariable Long roleId) {
        Role role = roleService.getRoleById(roleId);
        return Result.success(role);
    }

    /**
     * 获取所有角色
     * @return 角色列表
     */
    @GetMapping("/list")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR, DevOpsConstant.RoleCode.ADMINISTRATOR}, logical = Logical.OR)
    public Result<List<Role>> getAllRoles() {
        List<Role> roles = roleService.getAllRoles();
        return Result.success(roles);
    }

    /**
     * 获取可操作的角色列表
     * @return 角色列表
     */
    @GetMapping("/operation/list")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR}, logical = Logical.OR)
    public Result<List<Role>> getOperationRoles() {
        List<Role> roles = roleService.getOperationRoles();
        return Result.success(roles);
    }

    /**
     * 获取用户的角色列表
     * @param userId 用户ID
     * @return 角色列表
     */
    @GetMapping("/user/{userId}")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR, DevOpsConstant.RoleCode.ADMINISTRATOR}, logical = Logical.OR)
    public Result<List<Role>> getUserRoles(@PathVariable Long userId) {
        List<Role> roles = roleService.getUserRoles(userId);
        return Result.success(roles);
    }

    /**
     * 分配菜单权限给角色
     * @param roleId 角色ID
     * @param menuIds 菜单ID列表
     * @return 分配结果
     */
    @PostMapping("/assign/menus")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR}, logical = Logical.OR)
    public Result<Boolean> assignMenusToRole(@RequestParam Long roleId, @RequestBody List<Long> menuIds) {
        boolean assigned = roleService.assignMenusToRole(roleId, menuIds);
        return Result.success(assigned);
    }

    /**
     * 分配角色给用户
     * @param userId 用户ID
     * @param roleIds 角色ID列表
     * @return 分配结果
     */
    @PostMapping("/assign/user")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR, DevOpsConstant.RoleCode.ADMINISTRATOR}, logical = Logical.OR)
    public Result<Boolean> assignRolesToUser(@RequestParam Long userId, @RequestBody List<Long> roleIds) {
        boolean assigned = roleService.assignRolesToUser(userId, roleIds);
        return Result.success(assigned);
    }
}
